package com.morty.config.interceptor;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import com.baomidou.mybatisplus.annotation.DbType;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Intercepts({@Signature(
        type = Executor.class,
        method = "insert",
        args = {MappedStatement.class, Object.class}
)})
public class AutoSetInterceptor implements Interceptor {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    private static final String METHOD_PREPARE = "prepare";
    private static final String METHOD_SETPARAMETERS = "setParameters";
    private static final String METHOD_INSERT = "insert";
    private static DbType dbType;

    public AutoSetInterceptor() {
    }

    public Object intercept(Invocation invocation) throws Throwable {
        if(METHOD_INSERT.equals(invocation.getMethod().getName())) {
            MappedStatement statement = (MappedStatement)invocation.getArgs()[0];
            Object parameter = invocation.getArgs()[1];
            SqlCommandType sqlCommandType = statement.getSqlCommandType();
            Date d = new Date();
            if(parameter instanceof Map) {
                Iterator var6 = ((Map)parameter).values().iterator();

                while(var6.hasNext()) {
                    Object o = var6.next();
                    if(SqlCommandType.INSERT == sqlCommandType) {
//                        this.create(o, d);
                    } else if(SqlCommandType.UPDATE == sqlCommandType) {
//                        this.update(o, d);
                    }
                }
            } else if(SqlCommandType.INSERT == sqlCommandType) {
//                this.create(parameter, d);
            } else if(SqlCommandType.UPDATE == sqlCommandType) {
//                this.update(parameter, d);
            }
        }

        return invocation.proceed();
    }

//    public void create(Object o, Date d) {
//        try {
//            Field f1 = ReflectionUtils.getDeclaredField(o, createDateFieldName);
//            if(f1 != null) {
//                ReflectionUtils.setFieldValue(o, f1, d);
//            }
//
//            Field f2 = ReflectionUtils.getDeclaredField(o, updateDateFieldName);
//            if(f2 != null) {
//                ReflectionUtils.setFieldValue(o, f2, d);
//            }
//        } catch (Exception var5) {
//            this.log.error(var5.getMessage(), var5);
//        }
//
//    }
//
//    public void update(Object o, Date d) {
//        try {
//            Field f = ReflectionUtils.getDeclaredField(o, updateDateFieldName);
//            if(f != null) {
//                ReflectionUtils.setFieldValue(o, f, d);
//            }
//        } catch (Exception var4) {
//            this.log.error(var4.getMessage(), var4);
//        }
//
//    }

    public Object plugin(Object o) {
        return Plugin.wrap(o, this);
    }
}
